<?php
/**
 * @文件功能名称  : 管理员
 * @Author      : yaoGrace
 * @Date        : 2021/12/10 00:00
 */
class manageController extends adminController{
    public function __init()
    {
        parent::__init();
        $this->managerModel     =  model("manager");
        $this->roleModel        =  model("managerole");
    }

    /**
     * 后台人员界面
     */
    public function index(){
        //获取全部角色权限
        $this->roles =  $this->roleModel->getAllRoles();

        // 获取所有人员列表
      // $this->data =  $this->managerModel->getAllList();

       // 搜索
       $getSearchEmail = isset($this->gets[0])?trim($this->gets[0]):0;
       $getSearchRoleid = isset($this->gets[1])?trim($this->gets[1]):-1;
       $this->data =$this->managerModel->searchWhere($getSearchEmail,$getSearchRoleid);

    }

    /**
     * 修改页面
     */
    public function edit()
    {
        $id = $this->gets[0];
        $this->EditInfo = $this->managerModel->checkAuthByid($id);
        $this->roles = $this->roleModel->getAllRoles();

        //  执行编辑操作
        if (PG_POST) {
            //获取原始数据
            $OldReturnInfo = $this->managerModel->checkAuthByid($id);
            // 检查昵称是否修改
            $old_uname = $OldReturnInfo['manager_uname'];
            if ($old_uname != $_POST['manager_uname']) {
                $data['manager_uname'] = $_POST['manager_uname'];
            }
            // 检查邮箱是否有修改
            $old_email = $OldReturnInfo['email'];
            if ($old_email != $_POST['email']) {
                //检查已经修改过的次数
                if ($OldReturnInfo['changetimes'] > 0) {
                    return $this->json('仅一次修改邮箱机会，已无法再次修改!', 'error');
                } else {
                    $data['email'] = $_POST['email'];
                    $data['changetimes'] = 1;
                }
            }
            // 检查密码是否有修改
            $old_password = \phpGrace\tools\md5::getMd5($OldReturnInfo['password']);
            //新密码进行至 二次md5加密
            $NewPassword = md5(md5($_POST['password']));
            if (!empty($_POST['password'])) {
                //编辑页面输入新密码，判断 原密码还原到二次md5的值 和 新的密码 加密二次md5的值 对比，如果一致，则未修改。不一致则修改
                if ($old_password != $NewPassword) {
                    //存储到新密码的值 为 二次md5加密+随机位置加密
                    $data['password'] = \phpGrace\tools\md5::toMd5($_POST['password']);
                } else {
                    return $this->json("密码未修改 ！", 'error');
                }
            }
            // 检查角色是否改变
            $old_role_id = $OldReturnInfo['manager_role_id'];
            $new_Role_id = $_POST['manager_role_id'];
            if ($old_role_id != $new_Role_id) {
                $data['manager_role_id'] = $new_Role_id;
            }
            $this->managerModel->change($id, $data);
            $this->json("修改成功");
        }
    }

    /**
     * 添加页面
     */
    public function add(){
        $this->roles =  $this->roleModel->getAllRoles();
        //  执行添加操作
        if(PG_POST){
            $data = array(
                'manager_uname' =>  $_POST['manager_uname'],
                'email'         =>  $_POST['email'],
                'password'      =>  \phpGrace\tools\md5::toMd5($_POST['password']),
                'changetimes'   =>  0,
                'manager_login_time'=>time(),
                'manager_role_id'   =>$_POST['manager_role_id'],
                'manager_ip'    => "0.0.0.0",
                'create_time'   =>  time()
            );
            $addId =  $this->managerModel->add($data);
            if($addId){
                echo  $this->json("添加成功，即将跳转刷新....");
            }else{
                echo $this->json("添加失败","error");
            }
        }
    }

    /**
     * 搜索
     */
    public function search(){
        echo $this->managerModel->SearchManageReturnJson();
    }

    /**
     * 删除
     */
    public function del(){
        if(!PG_POST){$this->json("非法操作!",'error');}
        $id  = $_POST['id'];
        $res = $this->managerModel->del($id);
        if($res){
            $this->json("删除成功...");
        }else{
            $this->json("警告：删除失败!","error");
        }
    }

}